﻿// -----------------------------------------------------------------------
// <copyright file="MetodosDAO.cs" company="HP">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Datos
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using System.Data;
    
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetodosDAO
    {



        //Crear COmando
        public static SqlCommand CrearComandoSelect() 
        {
            string _cadenaConexion = CadenaDAO.CadenaConexion;
            SqlConnection _conexion = new SqlConnection();
            _conexion.ConnectionString = _cadenaConexion;
            SqlCommand _comando = new SqlCommand(); 
            _comando = _conexion.CreateCommand();
            _comando.CommandType = CommandType.Text;
            return _comando;
        }

        //Crear COmando Proc 
        public static SqlCommand CrearComandoProc(string SP)
        {
            string _cadenaConexion = CadenaDAO.CadenaConexion;
            SqlConnection _conexion = new SqlConnection(_cadenaConexion);
            SqlCommand _comando = new SqlCommand(SP, _conexion);
            _comando.CommandType = CommandType.StoredProcedure;
            return _comando;
        }

        public static DataSet EjecutarSP(SqlCommand comando) 
        {
            try
            {
                comando.Connection.Open(); 

                SqlDataAdapter dataAdapter = new SqlDataAdapter(comando);
                System.Data.DataSet dataSet = new System.Data.DataSet();
                dataAdapter.Fill(dataSet);

                return dataSet;
            }
            catch { throw; }
            finally
            {
                comando.Connection.Dispose();
                comando.Connection.Close();
            }
        }

        public static void EjecutarIU(SqlCommand comando)
        {
            try
            {
                comando.Connection.Open();
                comando.ExecuteScalar(); 
            }
            catch { throw; }
            finally
            {
                comando.Connection.Dispose();
                comando.Connection.Close();
            }
        }


        public static DataTable EjecutarComandoSelect(SqlCommand comando)
        {
            DataTable _tabla = new DataTable();
            try
            {
                comando.Connection.Open();
                SqlDataAdapter adaptador = new SqlDataAdapter();
                adaptador.SelectCommand = comando;
                adaptador.Fill(_tabla);
            }
            catch (Exception ex)
            { throw ex; }
            finally
            { comando.Connection.Close(); }
            return _tabla;
        }

    }
}
